# -*- encoding: utf-8 -*-
"""
Copyright (c) 2023 - present SKBioinfo
"""

import os, random, string
from datetime import timedelta
import yaml

config={}
base_dir=os.path.dirname(os.path.abspath(__file__))
with open(f"{base_dir}/config.yaml",'r') as f:
    config=yaml.safe_load(f)

class Config(object):
    # Set up the App SECRET_KEY
    SECRET_KEY  = os.getenv('SECRET_KEY', None)
    if not SECRET_KEY:
        SECRET_KEY = ''.join(random.choice( string.ascii_lowercase  ) for i in range( 32 ))

    # CDN Support Settings 
    CDN_DOMAIN  = os.getenv('CDN_DOMAIN' , None)
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    ## wangyi账户
    DB_ENGINE   = os.getenv('DB_ENGINE'   , config["DB_CONNECTION"]["DB_ENGINE"])
    DB_USERNAME = os.getenv('DB_USERNAME' , config["DB_CONNECTION"]["DB_USERNAME"])
    DB_PASS     = os.getenv('DB_PASS'     , config["DB_CONNECTION"]["DB_PASS"])
    DB_HOST     = os.getenv('DB_HOST'     , config["DB_CONNECTION"]["DB_HOST"])
    DB_PORT     = os.getenv('DB_PORT'     , config["DB_CONNECTION"]["DB_PORT"])
    DB_NAME     = os.getenv('DB_NAME'     , config["DB_CONNECTION"]["DB_NAME"])
    # try to set up a Relational DBMS
    if DB_ENGINE and DB_NAME and DB_USERNAME:
        try:
            # Relational DBMS: PSQL, MySql
            SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
                DB_ENGINE,
                DB_USERNAME,
                DB_PASS,
                DB_HOST,
                DB_PORT,
                DB_NAME
            )
            USE_SQLITE  = False
        except Exception as e:
            print('> Error: DBMS Exception: ' + str(e) )
            print('> Fallback to SQLite ')    
    if USE_SQLITE:
        print("use SQLITE")
        # This will create a file in <app> FOLDER
        # SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')

class DebugConfig(Config):
    DEBUG = True
    JWT_SECRET_KEY='my_secret_key'
    JWT_ALGORITHM='HS256'
    JWT_ACCESS_TOKEN_EXPIRES= timedelta(hours=1)
    PROPAGATE_EXCEPTIONS = True
    SQLALCHEMY_POOL_RECYCLE = 3600
    SQLALCHEMY_POOL_TIMEOUT = 20
    SQLALCHEMY_POOL_SIZE = 100

class ProductionConfig(Config):
    DEBUG = False
    ### Security ###
    SESSION_COOKIE_HTTPONLY = True
    REMEMBER_COOKIE_HTTPONLY = True
    REMEMBER_COOKIE_DURATION = 3600
    JWT_SECRET_KEY='my_secret_key'
    JWT_ALGORITHM='HS256'
    JWT_ACCESS_TOKEN_EXPIRES= timedelta(hours=1)
    PROPAGATE_EXCEPTIONS = True
    SQLALCHEMY_POOL_RECYCLE = 3600
    SQLALCHEMY_POOL_TIMEOUT = 20
    SQLALCHEMY_POOL_SIZE = 100
    BASE_DIR=config["BASE_INFO"]["BASE_DIR"]
    SAMPLE_LIST=config["BASE_INFO"]["SAMPLE_LIST"]
    SAMPLE_SHEET=config["BASE_INFO"]["SAMPLE_SHEET"]
    PIPE_USER=config["BASE_INFO"]["PIPE_USER"]
    PASSWORD=config["BASE_INFO"]["PASSWORD"]
    PATH_PREFIX=config["BASE_INFO"]["PATH_PREFIX"]
    RESULT_PREFIX=config["BASE_INFO"]["RESULT_PREFIX"]
    HOST_IP=config["BASE_INFO"]["HOST_IP"]
    ### pipe path on server ###
    PIPE_PATH=config["BASE_INFO"]["PIPE_PATH"]
    ### reportPath ###
    REPORT_PATH=config["BASE_INFO"]["REPORT_PATH"]
    CUSTOM_DATA_PATH=config["BASE_INFO"]["CUSTOM_DATA_PATH"]
    
# Load all possible configurations
config_dict = {
    'Production': ProductionConfig,
    'Debug'     : DebugConfig,
}
